package com.ioudi.digu.ds;


import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import com.ioudi.digu.dsdistribute.IDatasourceSelector;

public class RoutingDataSource extends AbstractRoutingDataSource {

	private IDatasourceSelector dsSelector;
	
	private String defaultDatasouce;
	
	public String getDefaultDatasouce()
	{
		return defaultDatasouce;
	}

	public void setDefaultDatasouce(String defaultDatasouce)
	{
		this.defaultDatasouce = defaultDatasouce;
	}

	public IDatasourceSelector getDsSelector()
	{
		return dsSelector;
	}

	public void setDsSelector(IDatasourceSelector dsSelector)
	{
		this.dsSelector = dsSelector;
	}

	@Override
	protected Object determineCurrentLookupKey() 
	{
		String db =  dsSelector.currentDatasouce();
		if(db == null)
		{
			db = defaultDatasouce;
		}
		return db;
	}
	
}
